package com.neouton.crm.refundRecords.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.common.util.ResponseAPI;
import com.neouton.crm.entity.FileUploadRecordVO;
import com.neouton.crm.pm.service.IPurchaseOrdeService;
import com.neouton.crm.refundRecords.entity.RefundRecordsVO;
import com.neouton.crm.refundRecords.service.RefundRecordsService;
import com.neouton.crm.sales.entity.SalesFileRelationVO;
import com.neouton.crm.sales.service.SalesFileRelationService;
import com.neouton.feginClient.FileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;

@RestController
@RequestMapping("refundRecords")
public class RefundRecordsController {

    @Resource
    private RefundRecordsService refundRecordsService;

    @Resource
    private FileService fileService;

    @Resource
    private SalesFileRelationService salesFileRelationService;

    @Resource
    private IPurchaseOrdeService purchaseOrdeService;

    /**
     * 新增或修改退款信息
     * @param refundRecords
     * @param token
     * @return
     */
    @PostMapping("/saveOrUpdateRefund")
    public ResponseAPI saveOrUpdateRefund(@RequestBody RefundRecordsVO refundRecords, @RequestHeader("token") String token) {
        int id = refundRecordsService.saveOrUpdateCustomer(refundRecords,token);
        return ResponseAPI.successData(id);
    }

    /**
     * 审批通过
     * @param refundRecords
     * @param token
     * @return
     */
    @PostMapping("/approveRefundRecords")
    public ResponseAPI approveRefundRecords(@RequestBody RefundRecordsVO refundRecords, @RequestHeader("token") String token){
        return ResponseAPI.successData(refundRecordsService.approveRefundRecords(refundRecords,token,false));
    }


    /**
     * 审批驳货
     * @param refundRecords
     * @param token
     * @return
     */
    @PostMapping("/rejectRefundRecords")
    public ResponseAPI rejectRefundRecords(@RequestBody RefundRecordsVO refundRecords, @RequestHeader("token") String token){
        return ResponseAPI.successData(refundRecordsService.rejectionRefundRecords(refundRecords,token));
    }

    /**
     * 获取列表
     * @param pageNum
     * @param pageSize
     * @param refundRecords
     * @param orderBy
     * @param token
     * @return
     */
    @PostMapping("/getRedList/{pageNum}/{pageSize}/{orderBy}")
    public ResponseAPI getRedListByPage(@PathVariable(value = "pageNum") int pageNum,
                                        @PathVariable(value = "pageSize") int pageSize,
                                        @RequestBody RefundRecordsVO refundRecords, @PathVariable(value = "orderBy") String orderBy, @RequestHeader("token") String token) {
        IPage<RefundRecordsVO> page = new Page<>(pageNum, pageSize, true);
        PageUtil<RefundRecordsVO> result = refundRecordsService.getRedListByPage(page, refundRecords,orderBy,token);
        return ResponseAPI.successData(result);
    }

    /**
     * 导出
     * @param response
     * @param refundRecords
     * @param token
     * @throws Exception
     */
    @PostMapping("/exportRef")
    public void exportRef(HttpServletResponse response,
                          @RequestBody RefundRecordsVO refundRecords,
                          @RequestHeader("token") String token) throws Exception{
        refundRecordsService.exportRef(response,refundRecords,token);
    }

    /**
     * 文件上传
     *
     * @param file      文件
     * @param id        退款记录ID
     * @param fileClass 文件类型
     * @return 上传的文件记录、用于前端构造fileList
     */
    @PostMapping("/uploadFile/{id}/{fileClass}")
    public ResponseAPI uploadFileForRefundRecord(@RequestParam("file") MultipartFile file,
                                                FileUploadRecordVO fileUploadRecord,
                                                @PathVariable("id") Integer id,
                                                @PathVariable("fileClass") Integer fileClass,
                                                @RequestHeader("token") String token) {
        // 调用远程上传服务
        FileUploadRecordVO fileUploadRecords = fileService.upload2Ftp(file, fileUploadRecord, token).getResult();
        // 将结果进行记录
        SalesFileRelationVO salesFileRelationVO = new SalesFileRelationVO();
        salesFileRelationVO.setSalesRelationId(id);
        salesFileRelationVO.setFileClass(fileClass);
        salesFileRelationVO.setCreator(AuthUtils.getUserId());
        salesFileRelationVO.setCreateDataTime(new Timestamp(new Date().getTime()));
        salesFileRelationVO.setFileId(fileUploadRecords.getId());
        salesFileRelationVO.setFileName(fileUploadRecords.getOriFileName());
        salesFileRelationVO.setContentType(fileUploadRecords.getContentType());
        salesFileRelationVO.setFlag(1);
        salesFileRelationService.save(salesFileRelationVO);
        return ResponseAPI.successData(fileUploadRecords);
    }


    /**
     * 获取退款总额通过付款id
     * @param refundRecords
     * @param token
     * @return
     */
    @PostMapping("/getPayMoneySum")
    public ResponseAPI getPayMoneySum(@RequestBody RefundRecordsVO refundRecords, @RequestHeader("token") String token) {
        Double refundRecordSum = refundRecordsService.getPayMoneySum(refundRecords,token);
        return ResponseAPI.successData(refundRecordSum);
    }

    /**
     * 通过项目ID查询所有采购单
     * @param projectId
     * @return
     */
    @PostMapping("/queryPurchaseOrdersByProjectId/{projectId}")
    public ResponseAPI queryPurchaseOrdersByProjectId(@PathVariable(value = "projectId") Integer projectId) {
        return ResponseAPI.successData(purchaseOrdeService.queryPurchaseOrdersByProjectId(projectId));
    }

    /**
     * 通过机会ID查询所有采购单
     * @param opportunityId
     * @return
     */
    @PostMapping("/queryPurchaseOrdersByOpportunityId/{opportunityId}")
    public ResponseAPI queryPurchaseOrdersByOpportunityId(@PathVariable(value = "opportunityId") Integer opportunityId) {
        return ResponseAPI.successData(purchaseOrdeService.queryPurchaseOrdersByOpId(opportunityId));
    }

    /**
     * 通过采购单ID查询所有付款记录
     * @param purchaseId
     * @return
     */
    @PostMapping("/getPayRecordsByPurchaseOrderIds/{purchaseId}")
    public ResponseAPI getPayRecordsByPurchaseOrderIds(@PathVariable(value = "purchaseId") Integer purchaseId) {
        return ResponseAPI.successData(refundRecordsService.getPayRecordsByPurchaseOrderIds(purchaseId));
    }

    /**
     * 删除退款记录
     * @param refundId
     * @return
     */
    @PostMapping("/deleteRefund/{refundId}")
    public ResponseAPI deleteRefund(@PathVariable(value = "refundId") Integer refundId, @RequestHeader("token") String token) {
        return ResponseAPI.successData(refundRecordsService.deleteRefund(refundId,token));
    }


    /**
     * 通过审批ID查询退款记录信息
     * @param approvalId
     * @return
     */
    @PostMapping("/getRefundInfoByApprovalId/{approvalId}")
    public ResponseAPI getRefundInfoByApprovalId(@PathVariable(value = "approvalId") Integer approvalId, @RequestHeader("token") String token) {
        return ResponseAPI.successData(refundRecordsService.getRefundInfoByApprovalId(approvalId,token));
    }
}
